package 中等;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class 区间交集 {
    public static void main(String[] args) {
        int[] arr = {1,5,4,6,7,2,1,6};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }



    public int[][] intervalIntersection(int[][] A, int[][] B) {
        int i = 0;
        int j = 0;
        int a1,a2,b1,b2;
        List<int[]> list = new ArrayList<int[]>();
        while (i < A.length && j < B.length){
            a1 = A[i][0];
            b2 = B[j][1];
            a2 = A[i][1];
            b1 = B[j][0];
            if (a1 <= b2 && a2 >= b1){
                list.add(new int[]{Math.max(a1,b1 ),Math.min(a2,b2 )});
            }
            if (a2 > b2){
                j++;
            }else {
                i++;
            }
        }
        return list.toArray(new int[list.size()][]);
    }
}
